package com.zzyl.nursing.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.NursingTask;
import com.zzyl.nursing.dto.NursingTaskDto;
import com.zzyl.nursing.service.INursingTaskService;
import com.zzyl.nursing.service.impl.NursingTaskServiceImpl;

import java.util.List;

import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.Api;

import javax.validation.constraints.Min;
import com.zzyl.nursing.dto.NursingTaskCancelDto;
import com.zzyl.nursing.dto.NursingTaskExecuteDto;
import com.zzyl.nursing.dto.NursingTaskUpdateTimeDto;

import javax.validation.Valid;
import io.swagger.annotations.ApiOperation;

/**
 * 护理任务Controller
 * 
 * @author ruoyi
 * @date 2024-09-12
 */
@RestController
@RequestMapping("/nursing/nursingTask")
@Api(tags = "护理任务的接口")
@Slf4j
public class NursingTaskController extends BaseController {

    @Autowired
    private INursingTaskService nursingTaskService;


    /**
     * 查询护理任务列表
    */
    @GetMapping("/list")
    public TableDataInfo list(NursingTaskDto dto) {

        startPage();
        List<NursingTask> page = nursingTaskService.list(dto);
        return getDataTable( page) ;
    }
    /**
     * 获取护理任务详细信息
     * */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(nursingTaskService.selectNursingTaskById(id));
    }
    /**
     * 取消任务
     */
    @PutMapping("/cancel")
    @ApiOperation("取消任务")
    public AjaxResult cancel(@Valid @RequestBody NursingTaskCancelDto cancelDto) {
        log.info("==========================================取消任务开始==========================================");
        log.info("接收到的DTO对象：{}", cancelDto);
        log.info("取消原因：{}，任务ID：{}", cancelDto.getReason(), cancelDto.getTaskId());
        
        // 参数校验
        if (cancelDto.getReason() == null || cancelDto.getReason().trim().isEmpty()) {
            return error("取消原因不能为空");
        }
        if (cancelDto.getTaskId() == null) {
            return error("护理任务ID不能为空");
        }
        
        nursingTaskService.cancel(cancelDto.getReason(), cancelDto.getTaskId());
        log.info("==========================================取消任务完成==========================================");
        return success("操作成功");
    }

    /**
     * 执行任务
     */
    @PutMapping("/do")
    @ApiOperation("执行任务")
    public AjaxResult execute(@Valid @RequestBody NursingTaskExecuteDto executeDto) {
        log.info("==========================================执行任务开始==========================================");
        log.info("接收到的DTO对象：{}", executeDto);
        log.info("任务ID：{}", executeDto.getTaskId());
        
        if (executeDto.getTaskId() == null) {
            return error("任务ID不能为空");
        }
        
        nursingTaskService.execute(executeDto);
        log.info("==========================================执行任务完成==========================================");
        return success("操作成功");
    }

    /**
     * 任务改期
     */
    @PutMapping("/updateTime")
    @ApiOperation("任务改期")
    public AjaxResult updateTime(@Valid @RequestBody NursingTaskUpdateTimeDto updateTimeDto) {
        log.info("==========================================任务改期开始==========================================");
        log.info("接收到的DTO对象：{}", updateTimeDto);
        log.info("任务ID：{}，新时间：{}", updateTimeDto.getTaskId(), updateTimeDto.getEstimatedServerTime());
        
        if (updateTimeDto.getTaskId() == null) {
            return error("任务ID不能为空");
        }
        if (updateTimeDto.getEstimatedServerTime() == null) {
            return error("预计服务时间不能为空");
        }
        
        nursingTaskService.updateTime(updateTimeDto);
        log.info("==========================================任务改期完成==========================================");
        return success("操作成功");
    }
    
}
